package cn.lentme.app.activity;

import static androidx.fragment.app.FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;

import cn.lentme.app.base.BaseActivity;
import cn.lentme.app.component.view.fragments.BlankFragment;

public class MainActivity extends BaseActivity {

    //组件
    Button btn_match;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private ViewPager viewPager;

    //工具
    public void setItem(int pos) {
        viewPager.setCurrentItem(pos);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.main_nav_vp);

        fragments.add(BlankFragment.newInstance(R.layout.frag_home));
        fragments.add(BlankFragment.newInstance(R.layout.frag_circles));
        fragments.add(BlankFragment.newInstance(R.layout.frag_profile));

        viewPager.setAdapter(myFragAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ((BottomNavigationView)findViewById(R.id.main_nav_bnv))
                        .getMenu().getItem(position)
                        .setChecked(true);
                if(position == 2) {
                    startActivity(new Intent(MainActivity.this, PaperHomeActivity.class));
                    viewPager.setCurrentItem(1);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        ((BottomNavigationView)findViewById(R.id.main_nav_bnv))
                .setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.nav_home:
                                viewPager.setCurrentItem(0);
                                break;
                            case R.id.nav_circles:
                                viewPager.setCurrentItem(1);
                                break;
                            case R.id.nav_user:
                                viewPager.setCurrentItem(2);
                                break;
                        }
                        return false;
                    }
                });
        viewPager.setCurrentItem(0);
        ((BottomNavigationView)findViewById(R.id.main_nav_bnv))
                .setVisibility(View.GONE);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private FragmentPagerAdapter myFragAdapter =
            new FragmentPagerAdapter(getSupportFragmentManager(),
                    FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
                @NonNull
                @Override
                public Fragment getItem(int position) {
                    return fragments.get(position);
                }

                @Override
                public int getCount() {
                    return fragments.size();
                }
            };

}
